<?php
class ControllerNewsArticle extends Controller {
	private $error = array();

	public function index() {
		$this->load->language('common/header');
		$this->load->language('news/article');
		
		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('news/type');
		$this->load->model('news/article');
		
		$this->getList();
	}

	public function insert() {
		$this->load->language('common/header');
		$this->load->language('news/article');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('news/type');
		$this->load->model('news/category');
		$this->load->model('news/article');
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_news_article->addArticle($this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');

			$this->redirect($this->url->link('news/article', 'token=' . $this->session->data['token'] . $this->url(), 'SSL'));
		}

		$this->getForm();
	}

	public function update() {
		$this->load->language('common/header');
		$this->load->language('news/article');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('news/type');
		$this->load->model('news/category');
		$this->load->model('news/article');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_news_article->editArticle($this->request->get['article_id'], $this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');

			$this->redirect($this->url->link('news/article', 'token=' . $this->session->data['token'] . $this->url(), 'SSL'));
		}

		$this->getForm();
	}

	public function delete() {
		$this->load->language('news/article');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('news/article');

		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $article_id) {
				$this->model_news_article->deleteArticle($article_id);
			}

			$this->session->data['success'] = $this->language->get('text_success');

			$this->redirect($this->url->link('news/article', 'token=' . $this->session->data['token'] . $this->url(), 'SSL'));
		}

		$this->getList();
	}

	private function getList() {
		if (isset($this->request->get['filter_title'])) {
			$filter_title = $this->request->get['filter_title'];
		} else {
			$filter_title = null;
		}

		if (isset($this->request->get['filter_type'])) {
			$filter_type = $this->request->get['filter_type'];
		} else {
			$filter_type = null;
		}

		if (isset($this->request->get['filter_category'])) {
			$filter_category = $this->request->get['filter_category'];
		} else {
			$filter_category = null;
		}
		
		if (isset($this->request->get['filter_month'])) {
			$filter_month = $this->request->get['filter_month'];
		} else {
			$filter_month = null;
		}

		if (isset($this->request->get['filter_status'])) {
			$filter_status = $this->request->get['filter_status'];
		} else {
			$filter_status = null;
		}

		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => false
		);
		
		
		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_content'),
   			'href'      => '#',
   			'separator' => ' :: '
   		);
		
		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_news'),
   			'href'      => '#',
   			'separator' => ' :: '
		);
			
		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_news'),
			'href'      => '#',
      		'separator' => ' :: '
		);

		$this->data['insert'] = $this->url->link('news/article/insert', 'token=' . $this->session->data['token'] . $this->url(), 'SSL');
		$this->data['delete'] = $this->url->link('news/article/delete', 'token=' . $this->session->data['token'] . $this->url(), 'SSL');
		 
		$page = isset($this->request->get['page']) ? $this->request->get['page'] : 1;
		$sort = isset($this->request->get['sort']) ? $this->request->get['sort'] : 'rna.date_added';
		$order = isset($this->request->get['order']) ? $this->request->get['order'] : 'DESC';

		$this->data['status_list'] = array(
			array(
				'name'	=> $this->language->get('text_enabled'),
	      		'value' => '1'
			),
			array(
				'name'	=> $this->language->get('text_disabled'),
	      		'value' => '0'
			)
		);
		
		$this->data['news_types'] = $this->model_news_type->getNewsTypes();
		
		$this->load->model('news/category');
		$data = array(
			'filter_type'	  => $filter_type,
			'sort'            => 'cd.name'
		);
		
		$this->data['categories'] = $this->model_news_category->getCategories($data);
		
		$data = array(
			'filter_title'	  => $filter_title, 
			'filter_type'	  => $filter_type,
			'filter_category' => $filter_category,
			'filter_month'	  => $filter_month,
			'filter_status'   => $filter_status,
			'sort'            => $sort,
			'order'           => $order,
			'start'           => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit'           => $this->config->get('config_admin_limit')
		);

		$this->load->model('setting/image');

		$template_total = $this->model_news_article->getTotalArticle($data);
		 
		$results = $this->model_news_article->getArticles($data);

		$this->data['news_articles'] = array();
		foreach ($results as $result) {
			$action = array();

			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('news/article/update', 'token=' . $this->session->data['token'] . '&article_id=' . $result['article_id'] . $this->url(), 'SSL')
			);

			if (!empty($result['image']) && file_exists(DIR_IMAGE . $result['image'])) {
				$image = $this->model_setting_image->resize($result['image'], 100, 100);
			} else {
				$image = $this->model_setting_image->resize('no_image.jpg', 100, 100);
			}

			$this->data['news_articles'][] = array(
				'article_id' 	=> $result['article_id'],
				'title'        	=> $result['title'],
				'category_name' => $result['category_name'],
      			'image'		  	=> $image,
				'date_added' 	=> date($this->language->get('date_format_short'), strtotime($result['date_added'])),
				'status'      	=> ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
				'selected'    	=> isset($this->request->post['selected']) && in_array($result['article_id'], $this->request->post['selected']),
				'action'      	=> $action
			);
		}

		// Mapping all language variables
		$this->data = array_merge($this->data, $this->language->getData());
		 
		$this->data['token'] = $this->session->data['token'];

		if (!isset($this->error['warning'])) {
			$this->data['error_warning'] = '';
		}

		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];

			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$this->data['sort_title'] = $this->url->link('news/article', 'token=' . $this->session->data['token'] . $this->url('rnad.title'), 'SSL');
		$this->data['sort_category'] = $this->url->link('news/article', 'token=' . $this->session->data['token'] . $this->url('rncd.name'), 'SSL');
		$this->data['sort_date'] = $this->url->link('news/article', 'token=' . $this->session->data['token'] . $this->url('rna.date_added'), 'SSL');
		$this->data['sort_status'] = $this->url->link('news/article', 'token=' . $this->session->data['token'] . $this->url('rna.status'), 'SSL');

		$pagination = new Pagination();
		$pagination->total = $template_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('news/article', 'token=' . $this->session->data['token'] . $this->url(), 'SSL');
		 
		$this->data['pagination'] = $pagination->render();

		$this->data['filter_title'] = $filter_title;
		$this->data['filter_type'] = $filter_type;
		$this->data['filter_category'] = $filter_category;
		$this->data['filter_month'] = $filter_month;
		$this->data['filter_status'] = $filter_status;

		$this->data['sort'] = $sort;
		$this->data['order'] = $order;

		$this->template = 'news/article_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);

		$this->response->setOutput($this->render());
	}

	private function getForm() {
		// Mapping all language variables
		$this->data = array_merge($this->data, $this->language->getData());
			
		if (!isset($this->error['warning'])) {
			$this->data['error_warning'] = '';
		}
	
 		if (isset($this->error['title'])) {
			$this->data['error_title'] = $this->error['title'];
		} else {
			$this->data['error_title'] = '';
		}
	
 		if (isset($this->error['header'])) {
			$this->data['error_header'] = $this->error['header'];
		} else {
			$this->data['error_header'] = '';
		}
	
 		if (isset($this->error['body'])) {
			$this->data['error_body'] = $this->error['body'];
		} else {
			$this->data['error_body'] = '';
		}
	
 		if (isset($this->error['category'])) {
			$this->data['error_category'] = $this->error['category'];
		} else {
			$this->data['error_category'] = '';
		}
		
		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => false
		);
		
		
		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_content'),
   			'href'      => '#',
   			'separator' => ' :: '
   		);
		
		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_news'),
   			'href'      => $this->url->link('news/article', 'token=' . $this->session->data['token'], 'SSL'),
   			'separator' => ' :: '
		);

		if (!isset($this->request->get['article_id'])) {
			$this->data['breadcrumbs'][] = array(
	       		'text'      => $this->language->get('heading_title_insert'),
				'href'      => '#',
	      		'separator' => ' :: '
			);
		} else {
			$this->data['breadcrumbs'][] = array(
				       		'text'      => $this->language->get('heading_title_update'),
							'href'      => '#',
				      		'separator' => ' :: '
			);
		}
		 
		if (!isset($this->request->get['article_id'])) {
			$this->data['action'] = $this->url->link('news/article/insert', 'token=' . $this->session->data['token'] . $this->url(), 'SSL');
		} else {
			$this->data['action'] = $this->url->link('news/article/update', 'token=' . $this->session->data['token'] . '&article_id=' . $this->request->get['article_id'] . $this->url(), 'SSL');
		}

		$this->data['cancel'] = $this->url->link('news/article', 'token=' . $this->session->data['token'] . $this->url(), 'SSL');

		$this->data['token'] = $this->session->data['token'];

		if (isset($this->request->get['article_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
			$article_info = $this->model_news_article->getArticle($this->request->get['article_id']);
		}
		
		if (isset($this->request->post['news_article_description'])) {
			$this->data['news_article_description'] = $this->request->post['news_article_description'];
		} elseif (isset($this->request->get['article_id'])) {
			$this->data['news_article_description'] = $this->model_news_article->getArticleDescriptions($this->request->get['article_id']);
		} else {
			$this->data['news_article_description'] = array();
		}
		
		if (isset($this->request->post['keyword'])) {
			$this->data['keyword'] = $this->request->post['keyword'];
		} elseif (!empty($article_info)) {
			$this->data['keyword'] = $article_info['keyword'];
		} else {
			$this->data['keyword'] = '';
		}
		
		if (isset($this->request->post['type_id'])) {
			$this->data['type_id'] = $this->request->post['type_id'];
		} elseif (!empty($article_info)) {
			$category_info = $this->model_news_category->getCategory($article_info['category_id']);
			$this->data['type_id'] = $category_info['type_id'];
		} else {
			$this->data['type_id'] = 1;
		}
		
		if (isset($this->request->post['sort_order'])) {
			$this->data['sort_order'] = $this->request->post['sort_order'];
		} elseif (!empty($article_info)) {
			$this->data['sort_order'] = $article_info['sort_order'];
		} else {
			$this->data['sort_order'] = 0;
		}
		 
		if (isset($this->request->post['status'])) {
			$this->data['status'] = $this->request->post['status'];
		} else if (!empty($article_info)) {
			$this->data['status'] = $article_info['status'];
		} else {
			$this->data['status'] = 1;
		}

				
		if (isset($this->request->post['image'])) {
			$this->data['image'] = $this->request->post['image'];
		} elseif (!empty($article_info)) {
			$this->data['image'] = $article_info['image'];
		} else {
			$this->data['image'] = '';
		}
		
		$this->load->model('setting/image');
		if (!empty($this->data['image']) && file_exists(DIR_IMAGE . $this->data['image'])) {
			$this->data['thumb'] = $this->model_setting_image->resize($this->data['image'], 100, 100);
		} else {
			$this->data['thumb'] = $this->model_setting_image->resize('no_image.jpg', 100, 100);
		}
		
		$this->data['no_image'] = $this->model_setting_image->resize('no_image.jpg', 100, 100);
		
		$this->data['news_types'] = $this->model_news_type->getNewsTypes();
				
		$this->load->model('news/category');
		$this->data['categories'] = array();
		foreach ($this->data['news_types'] as $news_type) {
			$data = array(
				'filter_type' => $news_type['type_id']
			);
			
			$this->data['categories'][$news_type['type_id']] = $this->model_news_category->getCategories($data);
		}
		
		if (isset($this->request->post['category_id'])) {
			$this->data['category_id'] = $this->request->post['category_id'];
		} elseif (isset($article_info)) {
			$this->data['category_id'] = $article_info['category_id'];
		} else {
			$this->data['category_id'] = array();
		}

		//load language
  		$this->load->model('setting/language');		
		$this->data['languages'] = $this->model_setting_language->getLanguages(array('supported' => $this->restaurant->getConfig()->get('config_supported_language')));

		$this->template = 'news/article_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);

		$this->response->setOutput($this->render());
	}

	private function validateForm() {
		if (!$this->user->hasPermission('modify', 'news/article')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		foreach ($this->request->post['news_article_description'] as $language_id => $value) {
			if ((utf8_strlen($value['title']) < 2) || (utf8_strlen($value['title']) > 255)) {
				$this->error['title'][$language_id] = $this->language->get('error_title');
			}
			if ((utf8_strlen($value['header']) < 1)) {
				$this->error['header'][$language_id] = $this->language->get('error_header');
			}
			if ((utf8_strlen($value['body']) < 1)) {
				$this->error['body'][$language_id] = $this->language->get('error_body');
			}
		}
		
		if(!isset($this->request->post['category_id'])
			|| (int)$this->request->post['category_id'] == 0
		){
			$this->error['category'] = $this->language->get('error_category');
		}
		
		if ($this->error && !isset($this->error['warning'])) {
			$this->error['warning'] = $this->language->get('error_warning');
		}

		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}

	private function validateDelete() {
		if (!$this->user->hasPermission('modify', 'news/article')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		if ($this->error && !isset($this->error['warning'])) {
			$this->error['warning'] = $this->language->get('error_warning');
		}

		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}

	private function url($sort = ''){
		$url = "";

		if (isset($this->request->get['filter_title'])) {
			$url .= '&filter_title=' . $this->request->get['filter_title'];
		}

		if (isset($this->request->get['filter_type'])) {
			$url .= '&filter_type=' . $this->request->get['filter_type'];
		}

		if (isset($this->request->get['filter_category'])) {
			$url .= '&filter_category=' . $this->request->get['filter_category'];
		}
	
		if (isset($this->request->get['filter_month'])) {
			$url .= '&filter_month=' . $this->request->get['filter_month'];
		}
		
		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}

		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}

		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		if(!empty($sort)) {
			if ($order == 'ASC') {
				$url .= '&order=DESC';
			} else {
				$url .= '&order=ASC';
			}

			$url .= '&sort=' . $sort;
		} else {

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			} else {
				$url .= '&sort=t.name';
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}
		}

		$url .= '&page=' . $page;

		return $url;
	}
}
?>